package in.muton.edushare.components.tasks.requests;

import in.muton.edushare.EduShareSession;

import com.edushare.common.dao.GroupsMembersDAO;
import com.edushare.common.util.EntityUtil;
import com.edushare.common.util.EntityUtil.ENTITY;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.GroupMembers;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class AcceptDeclineUtil {
	
	/**
	 * Use this method when you are not sure the type of entity that you are handling.
	 * This is handy and flexible compared to other acceptDeclineUser* methods in this class
	 * @param <T>
	 * @param u
	 * @param entity
	 * @param isAccept
	 */
	public static final <T extends IFileUploadable> void acceptDeclineUser(Users u, T entity, boolean isAccept){
		switch (EntityUtil.getEntityType(entity)) {
		
		case UNIVERSITY:
			acceptDeclineUserInUniversity(u, (Universities)entity, isAccept);
			break;
		
		case DEPARTMENT:
			acceptDeclineUserInDepartment(u, (Departments)entity, isAccept);
			break;
		
		case COURSE:
			acceptDeclineUserInCourse(u, (Courses)entity, isAccept);
			break;
			
		case GROUP:
			acceptDeclineUserInGroup(u, (Groups)entity, isAccept);
			break;
		case USER:
			acceptDeclineUserAsFriend(u, (Users)entity, isAccept);
			break;
			
		default:
			break;
		}
	}
	
	/**
	 * TODO: implement
	 * @param u
	 * @param univ
	 * @param isAccept
	 */
	public static final void acceptDeclineUserInUniversity(Users u, Universities univ, boolean isAccept){}
	
	/**
	 * TODO: implement
	 * @param u
	 * @param univ
	 * @param isAccept
	 */
	public static final void acceptDeclineUserInDepartment(Users u, Departments dept, boolean isAccept){}
	
	/**
	 * TODO: implement
	 * @param u
	 * @param univ
	 * @param isAccept
	 */
	public static final void acceptDeclineUserInCourse(Users u, Courses courses, boolean isAccept){}
	
	/**
	 * Activate or remove user-group link
	 * @param u
	 * @param group
	 * @param isAccept
	 */
	public static final void acceptDeclineUserInGroup(Users u, Groups group, boolean isAccept){
		GroupMembers member = null;
		for(GroupMembers gm: u.getGroupMemberships()){
			if(gm.getGroup().getGroupId() == group.getGroupId()){
				member = gm;
				break;
			}
		}
		
		if(member == null)
			return;
		
		if(isAccept){
			member.setStatus(GroupMembers.STATUS_ACTIVE);
		}else{
			new GroupsMembersDAO(EduShareSession.get().getSessionORMInterface()).remove(member);
		}
		
		EduShareSession.get().getSessionORMInterface().commitAllChanges();
		
	}
	
	/**
	 * TODO: implement
	 * @param u
	 * @param univ
	 * @param isAccept
	 */
	public static final void acceptDeclineUserAsFriend(Users requestingUser, Users requestedUser, boolean isAccept){}
	
	
	
}
